package com.itheima.news.controller;

import com.itheima.news.enums.NewsBehaviorEnum;
import com.itheima.news.pojo.News;
import com.itheima.news.service.NewsService;
import com.itheima.news.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻管理Controller
 * @author xiao1.wang@dmall.com
 * @date 2019-07-10 22:30:10
 */
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @GetMapping("/list")
    @ResponseBody
    public List<News> list() {
        return newsService.list();
    }

    @GetMapping("/add")
    @ResponseBody
    public String add() {
        News news = new News();
        news.setTitle("AAA");
        news.setSubTitle("aaa");
        news.setImgUrl("https://www.baidu.com");
        news.setBehavior(NewsBehaviorEnum.RECOMMEND.getValue());
        news.setSketch("这是一篇好新闻");
        newsService.add(news);
        return "success";
    }

    @PostMapping("/page")
    @ResponseBody
    public Page<News> findByPage(News news, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<News> p = newsService.findByPage(news, page, size);
        return p;
    }
}
